package init

import (
	"chess_platform/internal/alisdk"
	"chess_platform/internal/common"
	"chess_platform/internal/ipay"
	"chess_platform/internal/token"
	"chess_platform/models/pc"
	_ "chess_platform/time_task"
	"encoding/json"
	"fmt"
	"io/ioutil"
)

func init() {
	token.SetKey(common.Secret.TokenKey)
	token.SetExpire(common.Secret.TokenExpire)

	//初始化阿里云短信
	alisdk.InitAliSMS(common.Sms.AccessKeyIdSms, common.Sms.AccessKeySecretSms)

	//初始化oss
	alisdk.InitAliOSS(common.OssKey.AccessKeyIdOss, common.OssKey.AccessKeySecretOss,
		common.OssConf.OssUrlRoot, common.OssConf.OssBucketName, common.OssConf.OssRoleAcs, common.OssConf.OssRegion)

	//初始化爱贝支付
	var err error
	common.H5IPay, err = ipay.InitIPay(H5IPayAppId, H5PrivateKEY, H5PublicKEY)
	if err != nil {
		msg := fmt.Sprintf("初始化H5平台爱贝支付失败:%v", err)
		common.LogPc.Error(msg)
		panic(msg)
	}
	common.ClientIPay, err = ipay.InitIPay(ClientIPayAppId, ClientPrivateKEY, ClientPublicKEY)
	if err != nil {
		msg := fmt.Sprintf("初始化Client端爱贝支付失败:%v", err)
		common.LogPc.Error(msg)
		panic(msg)
	}

	//err := InitMenu()
	//if err != nil {
	//	fmt.Println("---------------------------------err:", err)
	//}
}

type Menu struct {
	Name       string `json:"name"`
	ModuleName string `json:"module_name"`
	Child      []Menu `json:"child"`
}

func InitMenu() error {
	dataStream, err := ioutil.ReadFile("conf/menu.json")
	if err != nil {
		return err
	}
	var menu []Menu
	err = json.Unmarshal(dataStream, &menu)
	if err != nil {
		return err
	}
	o := common.NewDBOrm(common.DBChessCenter)
	for _, v := range menu {
		//第一级menu
		firstPermission := map[string]interface{}{
			"name":        v.Name,
			"parent_id":   0,
			"module_name": v.ModuleName,
		}
		id, _ := pc.NewMenu(firstPermission, o)
		for _, vv := range v.Child {
			//第二级menu
			secondPermission := map[string]interface{}{
				"name":        vv.Name,
				"module_name": vv.ModuleName,
				"parent_id":   id,
			}
			pc.NewMenu(secondPermission, o)

		}
	}
	return nil
}

const (
	H5IPayAppId     = "3019378920"
	ClientIPayAppId = "301941765"
)

var (
	H5PublicKEY = []byte(`-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDL6a7UQm/Yw0iwZJWoyykCN7HF
bsYWpMYjG3WttAgDB5BWy8+BQePZQT/t8VTNyKtBUiGKOcyV4j7VA/dKeLgfn87J
QuKkrilu031R/jKlt13xcju4m5BtGn+VsExtOiva0awR3kiX528sD/yJ8oSqv5ew
QVLWyCUPukTmNN6qoQIDAQAB
-----END PUBLIC KEY-----`)

	H5PrivateKEY = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCTYtaKu2iQJsANaP0dGnjVNsY0OOk/SWSdbqJoCa487nrk+9gZ
lhEansUBzWUYdR9wjtGgm5dfGZS1z1c51PXvVOQ3jIImkqIKZg7eyGIuiXiENu8r
tEwipPy6Wgu2uOrHGuLiRBlBV7gtE+ANWtyuiMhMWj5sokfaeAUiJ7OQmQIDAQAB
AoGAFCAZ9aWzmIOCFBKzBWdY+pxZIkZnKzolYA38ZUJ1JWLXKy9sicnhGgZ2FlHq
rRVJLsCBxXGZkOXYVZ8zZPU8g7U/BZ9JGP2hYUPs4HWtHvUJ7p1fpd2wtryhxMrm
BVNy473ZTI7Z7HvbG+DkKj7qdW6VIB30EWDAhtREDimnkRECQQDfNleJounPNLPF
2TNGjuemBNVxXi3oDhUVQ+Lg2tE6aU5XHoF2kpd19wm5oEDuAVzBl2VWIpiUPb7f
sWvkc3ddAkEAqQkiOgE+HZzp/352bRj5Evh0om4VpWDMNtO4PpwxdbBS5aops8uC
jdzzhbrcKBEBBUenOKACfQkeWOGH7arWbQJBAKsSRvzhszhvN+jGkpndJIUE52E/
wQc848Osh9G1Yu3Vj4kNhEOQDi0G7JkrzRecS67DjZL6aN4UhJmErSAlrrECQHbJ
c2L90o/u4EXqsgBy5z4cmaI36EBCqN/youx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